import { BaseCommand } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { classTransformToObj, objTransformToClass } from '../decorators/toJSON';
import { Cell } from '../cors/cell';
export interface IValue {
    row: number;
    col: number;
    value: boolean|string|number|null;
}
export class ValueChangeCommand extends BaseCommand {
    protected oldValues: IValue[] = [];
    constructor(
        protected tableDatas: TableDatas,
        protected values: IValue[]
    ) {
        super();
    }
    execute(): boolean {
        this.oldValues = [];
        for(let item of this.values) {
            if(this.tableDatas.cellIsExist(item.row, item.col)) {
                let cell = this.tableDatas.getCell(item.row, item.col) as Cell;
                this.oldValues.push({
                    row: item.row,
                    col: item.col,
                    value: cell.value.getOriginValue()
                });
                
            } else {
                this.oldValues.push({
                    row: item.row,
                    col: item.col,
                    value: null
                });
            }
            this.tableDatas.setCellValue(item.row, item.col, item.value);
        }
        return true;
    }
    redo(): boolean {
        for(let item of this.oldValues) {
            this.tableDatas.setCellValue(item.row, item.col, item.value);
        }
        return true;
    }
}